package bb.lanxing.lib.devices.base;

import android.content.Context;
//import com.igexin.push.config.c;
import bb.lanxing.lib.devices.api.ConnectionListener;
import bb.lanxing.lib.devices.api.DeviceManager;
import bb.lanxing.lib.devices.api.PeerDevice;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.core.scanner.BleSmartDeviceScanner;
import bb.lanxing.lib.devices.core.scanner.DeviceScanner;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;
import bb.lanxing.lib.devices.utils.AntPlusUtils;
import bb.lanxing.lib.devices.utils.DeviceUtils;
import java.lang.Thread;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

//@Deprecated
class AutoConnectorJava extends Thread implements ConnectionListener,
        DeviceScanner.ScannerCallback<SmartDevice>, Thread.UncaughtExceptionHandler {
    private final List<SmartDevice> mBoundDeviceList;
    private final Object mConnectionLock = new Object();
    private List<SmartDevice> mConnectionQueue = Collections.synchronizedList(new ArrayList<>());
    private final AbstractXZDeviceManager mDeviceManager;
    private boolean mIsRunning;
    private int mRunTimes;
    private DeviceScanner mScanner;

    @Override
    public void onStartScan() {
    }

    @Override
    public void onStopScan() {
    }

    AutoConnectorJava(AbstractXZDeviceManager abstractXZDeviceManager, List<SmartDevice> list) {
        this.mBoundDeviceList = list;
        this.mDeviceManager = abstractXZDeviceManager;
        setUncaughtExceptionHandler(this);
    }

    @Override
    public void run() {
        if (this.mDeviceManager == null) {
            return;
        }
        Context applicationContext = DeviceContext.getApplicationContext();
        this.mDeviceManager.registerConnectionStateListener(this);
        this.mScanner = new BleSmartDeviceScanner(applicationContext, this, DeviceHelper.getDeviceProvider());
        while (this.mIsRunning && this.mRunTimes < 8) {
            if (LocalDeviceManager.checkBluetooth(applicationContext) || AntPlusUtils.isAntPlusAvailable(applicationContext)) {
                this.mScanner.startScan();
                synchronized (this.mConnectionLock) {
                    try {
                        this.mConnectionLock.wait(10000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                this.mScanner.stopScan();
                while (!this.mConnectionQueue.isEmpty() && this.mIsRunning) {
                    PeerDevice createDeviceIfNeed = this.mDeviceManager.createDeviceIfNeed(this.mConnectionQueue.remove(0));
                    if (createDeviceIfNeed != null) {
                        createDeviceIfNeed.connect();
                        synchronized (this.mConnectionLock) {
                            try {
                                this.mConnectionLock.wait(/*c.B*/600000L);
                            } catch (InterruptedException unused) {
                            }
                        }
                    }
                }
            }
            this.mRunTimes++;
            this.mConnectionQueue.clear();
            if (this.mBoundDeviceList.isEmpty()) {
                break;
            }
            synchronized (this.mConnectionLock) {
                try {
                    if (this.mIsRunning) {
                        this.mConnectionLock.wait(10000L);
                    }
                } catch (InterruptedException unused2) {
                }
            }
        }
        this.mScanner.release();
        this.mScanner = null;
        this.mDeviceManager.unregisterConnectionStateListener(this);
        this.mConnectionQueue.clear();
        this.mConnectionQueue = null;
    }

    @Override
    public void onConnectionStateChanged(SmartDevice smartDevice, int i, int i2) {
        if (i == DeviceManager.STATE_CONNECTED) {
            onDeviceConnected(smartDevice);
        } else if (i == DeviceManager.STATE_DISCONNECTED) {
            onDeviceDisconnected(smartDevice);
        }
    }

    public void onDeviceConnected(SmartDevice smartDevice) {
        synchronized (this.mConnectionLock) {
            this.mBoundDeviceList.remove(smartDevice);
            ListIterator<SmartDevice> listIterator = this.mBoundDeviceList.listIterator();
            DeviceHelper.getDeviceProvider();
            while (listIterator.hasNext()) {
                SmartDevice next = listIterator.next();
                if (next.getType() == smartDevice.getType() || (DeviceUtils.isCadenceDevice(smartDevice) && DeviceUtils.isCadenceDevice(next))) {
                    if (!DeviceUtils.isDualModeDevice(smartDevice.getName()) || !DeviceUtils.shouldConnectToDualModeDevice(next.getType(), next.getName(), DeviceHelper.getDeviceApplication())) {
                        listIterator.remove();
                        this.mConnectionQueue.remove(next);
                    }
                }
            }
            this.mConnectionLock.notifyAll();
        }
    }

    public void onDeviceDisconnected(SmartDevice smartDevice) {
        synchronized (this.mConnectionLock) {
            this.mConnectionLock.notifyAll();
        }
    }

    @Override
    public synchronized void start() {
        if (this.mIsRunning) {
            synchronized (this.mConnectionLock) {
                this.mConnectionLock.notifyAll();
            }
            return;
        }
        this.mIsRunning = true;
        super.start();
    }

    void quit() {
        synchronized (this.mConnectionLock) {
            this.mIsRunning = false;
            interrupt();
        }
    }

    public boolean isRunning() {
        return this.mIsRunning;
    }

    @Override
    public void onDevice(SmartDevice smartDevice) {
        for (SmartDevice smartDevice2 : this.mBoundDeviceList) {
            if (smartDevice2.equals(smartDevice) && !this.mConnectionQueue.contains(smartDevice2)) {
                this.mConnectionQueue.add(smartDevice2);
                if (this.mConnectionQueue.size() == this.mBoundDeviceList.size()) {
                    synchronized (this.mConnectionLock) {
                        this.mConnectionLock.notifyAll();
                    }
                } else {
                    continue;
                }
            }
        }
    }

    @Override
    public void uncaughtException(Thread thread, Throwable th) {
        DeviceScanner deviceScanner = this.mScanner;
        if (deviceScanner != null) {
            deviceScanner.release();
            this.mScanner = null;
        }
    }
}
